package com.abetsy.vben.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.abetsy.vben.dao.GlobalConfigMapper;
import com.abetsy.vben.dao.SysUserMapper;
import com.abetsy.vben.entity.GlobalConfig;
import com.abetsy.vben.entity.SysUser;
import com.abetsy.vben.param.LoginParam;
import com.abetsy.vben.param.RegisterParam;
import com.abetsy.vben.service.SysUserService;
import com.abetsy.vben.utils.i18n.VbenException;
import com.abetsy.vben.vo.UserInfoVo;
import com.abetsy.vben.vo.LoginVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private GlobalConfigMapper globalConfigMapper;

    @Override
    public LoginVo login(LoginParam param) {
        QueryWrapper<SysUser> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username", param.getUsername());
        SysUser selectOne = sysUserMapper.selectOne(userQueryWrapper);
        if (selectOne == null) {
            VbenException.locale("account.no.found", param.getUsername());
        }
        assert selectOne != null;
        if (!BCrypt.checkpw(selectOne.getSalt()+param.getPassword(), selectOne.getPassword())) {
            VbenException.locale("account.password.error");
        }
        if (!selectOne.isEnabled()){
            VbenException.locale("account.disabled", param.getUsername());
        }
        StpUtil.login(selectOne.getUid());
        LoginVo loginVo = new LoginVo();
        loginVo.setAccessToken(StpUtil.getTokenValue());
        loginVo.setId(selectOne.getUid());
        loginVo.setPassword(param.getPassword());
        loginVo.setRoles(Collections.singletonList("super"));
        loginVo.setRealName(StrUtil.isEmpty(selectOne.getNickname()) ? selectOne.getUsername() : selectOne.getNickname());
        loginVo.setUsername(selectOne.getUsername());
        return loginVo;
    }

    @Override
    public UserInfoVo info() {
        QueryWrapper<SysUser> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("uid", Integer.parseInt(StpUtil.getLoginId().toString()));
        SysUser selectOne = sysUserMapper.selectOne(userQueryWrapper);
        StpUtil.login(selectOne.getUid());
        UserInfoVo infoVo = new UserInfoVo();
        infoVo.setId(selectOne.getUid());
        infoVo.setTag("SVIP");
        infoVo.setDescription("内测用户");
        infoVo.setRoles(Collections.singletonList("super"));
        infoVo.setRealName(StrUtil.isEmpty(selectOne.getNickname()) ? selectOne.getUsername() : selectOne.getNickname());
        infoVo.setUsername(selectOne.getUsername());
        return infoVo;
    }

    @Override
    public List<String> codes() {
        return Arrays.asList("AC_100100", "AC_100110", "AC_100120", "AC_100010");
    }

    @Override
    public Object userAdd(RegisterParam registerParam) {
        QueryWrapper<SysUser> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username", registerParam.getUsername());
        SysUser selectOne = sysUserMapper.selectOne(userQueryWrapper);
        if (selectOne != null) {
            VbenException.locale("account.is.exists", registerParam.getUsername());
        }
        SysUser sysUser = new SysUser();
        sysUser.setNickname(registerParam.getNickname());
        sysUser.setUsername(registerParam.getUsername());
        sysUser.setEnabled(true);
        String salt = BCrypt.gensalt();
        sysUser.setSalt(salt);
        sysUser.setPassword(BCrypt.hashpw(salt+registerParam.getPassword(), salt));
        sysUserMapper.insert(sysUser);
        // 新增用户结束后要同时添加全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setUserId(Math.toIntExact(sysUser.getUid()));
        globalConfig.setProxyActivate(true);
        globalConfig.setProxyPort(7890);
        globalConfig.setProxyIp("127.0.0.1");
        globalConfigMapper.insertOrUpdate(globalConfig);
        return null;
    }

}
